package pers.cz.encrypt;

import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.Base64;

/**
 * @program: PostGirl-panent
 * @description: base64加解密工具
 * @author: Cheng Zhi
 * @create: 2023-06-23 11:21
 **/
public class Base64Utils {

    public static void base2Img(String imgeStr, File file) {

        // 将图片转化为base64
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgeStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(file);
            out.write(b);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String img2Base(File file) {

        try {
            InputStream inputStream = new FileInputStream(file);
            int count = 0;
            while (count == 0) {
                count = inputStream.available();
            }
            byte[] buffer = new byte[count];
            inputStream.read(buffer);

            // 对字节数组Base64编码
            Base64.Encoder encode = Base64.getEncoder();
            return encode.encodeToString(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public static void main(String[] args) {
        //File file = new File("C:\\Users\\cz\\Desktop\\简历\\证件照.jpg");
        //String s = img2Base(file);
        //System.out.println(s);
        String aa = "";
        int index = aa.indexOf("base64,");
        String substring = aa.substring(index+7,aa.length());
        System.out.println(substring);
    }
}
